package com.itboye.mengwu.buy.ui;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.provider.ContactsContract.CommonDataKinds.Relation;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.adapter.AllOrderQueryAdapter;
import com.itboye.mengwu.data.model.OrderBean;
import com.itboye.mengwu.data.model.OrderQueryModel;
import com.itboye.mengwu.data.presenter.OrderPresenter;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;

public class OrderFragment extends BaseFragment implements Observer,
		IXListViewListener {
	TextView txt_title;
	OrderPresenter orderPresenter;
	private int pageNo = 1;
	private ArrayList<OrderQueryModel.OrderList> orderQueryModel = new ArrayList<OrderQueryModel.OrderList>();

	XListView xlistView;
	String uid;
	private OrderBean model;
	private AllOrderQueryAdapter adapter;
	private int status;
	RelativeLayout title_bar;
	public static String REFRESH_UI = "refresh";
	RelativeLayout nodata;

	public OrderFragment(int status) {
		this.status = status;
	}

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_allorder;
	}

	@Override
	public void onMyActivityCreated() {
		// TODO Auto-generated method stub
		title_bar.setVisibility(View.GONE);
		IntentFilter filter = new IntentFilter(REFRESH_UI);
		getActivity().registerReceiver(receiver, filter);
		uid = SPUtils.get(getActivity(), null, SpPublicParams.UID, "") + "";
		adapter = new AllOrderQueryAdapter(orderQueryModel, getActivity());
		xlistView.setAdapter(adapter);
		showProgressDialog("加载中", true);
		xlistView.setXListViewListener(this);
		xlistView.setPullLoadEnable(true);
		xlistView.setPullRefreshEnable(true);
		xlistView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), AllOrderDetail.class);
				intent.putExtra("model", orderQueryModel.get(position - 1));
				intent.putExtra("id", orderQueryModel.get(position - 1).getId());
				startActivity(intent);
			}
		});
		orderPresenter = new OrderPresenter(this);
		model = new OrderBean();
		model.setStatus(status + "");
		model.setUid(uid);

		model.setPageNo(pageNo + "");
		orderPresenter.getInfo(model);
	}

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			model = new OrderBean();
			model.setStatus(status + "");
			model.setUid(uid);

			model.setPageNo(pageNo + "");
			orderPresenter.getInfo(model);
		}
	};

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (pageNo == 1) {
				orderQueryModel.clear();
			}
			orderQueryModel.addAll(((OrderQueryModel) entity.getData())
					.getList());
			if (orderQueryModel.size() == 0) {
				nodata.setVisibility(View.VISIBLE);
				xlistView.setVisibility(View.GONE);

			} else {
				nodata.setVisibility(View.GONE);
				xlistView.setVisibility(View.VISIBLE);
			}

			adapter.notifyDataSetChanged();
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
		stopLoad();
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		pageNo = 1;
		model.setStatus(status + "");
		model.setUid(uid);
		model.setPageNo(pageNo + "");
		orderPresenter.getInfo(model);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		pageNo++;
		model.setStatus(status + "");
		model.setUid(uid);
		model.setPageNo(pageNo + "");
		orderPresenter.getInfo(model);
	}

	public void stopLoad() {
		xlistView.stopLoadMore();
		xlistView.stopRefresh();
	}

}
